package top.xcxdl.rural.utils;

import com.alibaba.fastjson.JSONObject;
import top.xcxdl.rural.utils.model.request.SmsSendRequest;
import top.xcxdl.rural.utils.model.request.SmsVariableRequest;
import top.xcxdl.rural.utils.model.response.SmsSendResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : Ding
 * @create 2022/3/31 17:09
 */
public class ApiServe {
    public static String invokeShenfen(String name, String idNum) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("appId", "N7724243");
        params.put("appKey", "0mh5Ena2j");
        params.put("name", name);
        params.put("idNum", idNum);
        String result = UrlUtil.sendPost("https://api.253.com/open/idcard/id-card-auth", params);
        // 解析json,并返回结果
        return result;
    }

    public static String invokeShortMsg(Integer msgtype, String phone, List<JSONObject> BIAN){
        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = "http://smssh1.253.com/msg/variable/json";
        // 短信内容
        String MSG = "";
        String params = "";
        switch (msgtype){
            case 1:
                //处理学生认证短信
                String temp = "请登录以查看，";
                params = phone+","+BIAN.get(0).get("type")+","+temp;
                MSG = "【西智生活】你的认证申请{$var}，{$var}如遇到问题，请联系客服。";
                break;
            case 2:
                //有变量-师傅
                params = phone+",";
                params += (BIAN.get(0).get("sj")+",");
                params += (BIAN.get(0).get("position_s")+"（目的地："+BIAN.get(0).get("position_e")+"）"+",");
                params += (BIAN.get(0).get("begin_time")+",");
                //插入乘车人信息给司机
                params += "(";
                for (JSONObject jsonObject : BIAN) {
                    params += jsonObject.get("uname")+":"+jsonObject.get("utel")+"、";
                }
                params = params.substring(0, params.length() - 1);
                params += ")";
                System.err.println(params);
                MSG = "【kapool】{$var}师傅您好，您已收到新的发车订单，请您尽快前往乘车点•{$var}。发车时间：{$var}，乘车人信息：{$var}。";
                break;
            case 3:
                //短信
                params = phone+"," + String.valueOf(BIAN.get(0).get("code"));
                MSG = "【西智生活】你的验证码为：{$var}，（5分钟内有效），切勿泄露，如非本人操作请忽略。";
                /*
                {"code":"0","failNum":"0","successNum":"1","msgId":"22041011043000202211000001776741",
                "time":"20220410110430","errorMsg":""}
                 */
                break;
            default:break;
        }
        //状态报告
        String report= "true";

        SmsVariableRequest smsSingleRequest = new SmsVariableRequest("N7724243", "0mh5Ena2j", MSG,params,report);
        String requestJson = JSONObject.toJSONString(smsSingleRequest);
        String response = SmsServe.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        SmsSendResponse smsSingleResponse = JSONObject.parseObject(response, SmsSendResponse.class);
        return response;
    }

    public static String invokeShortMsgStatic(Integer msgtype, String phone){
        //请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
        String smsSingleRequestServerUrl = "http://smssh1.253.com/msg/v1/send/json";
        String tel = "";
        String MSG = "";
        switch (msgtype){
            case 1:{
                //客服通知短信-常量
                tel = phone;
                MSG = "【kapool】您有一条订单信息需要处理";
                break;
            }
            default:break;
        }
        String report = "true";
        SmsSendRequest smsSingleRequest = new SmsSendRequest("N0972552", "FI8Qo8eK2ec08c", MSG,tel,report);
        String requestJson = JSONObject.toJSONString(smsSingleRequest);
        String response = SmsServe.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        SmsSendResponse smsSingleResponse = JSONObject.parseObject(response, SmsSendResponse.class);
        return response;
    }

    public static String getShortMessage(String code, String phoneNumber) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("account", "N7724243");
        params.put("password", "0mh5Ena2j");
        params.put("msg", "【ceshi1】test"+code);
        params.put("phone", phoneNumber);
        String result = UrlUtil.sendPost("http://smssh1.253.com/msg/v1/send/json", params);
        return result;
        // 解析json,并返回结果
    }
}
